package org.bbs.demo.utils.jwtinterepter;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.bbs.demo.advice.TokenException;
import org.bbs.demo.utils.jwtutil.JWTutil;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


public class JWTinterepter  implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //如果不是映射到方法
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        //获取请求头中的令牌
        String token = request.getHeader("token");
        System.out.println("token:" + token);

        if (token == null) {
            throw new TokenException("无token 请验证登录");
        }

        JWTutil.isExpiration(token);
        String userid = JWTutil.getUserID(token);

        return true;
    }
}
